<?php
/**
 * | @author 韩威兵^life <1877361302@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019 HQ
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/5/3 11:24 jiahe_new Order.php 韩威兵^life $
 * +----------------------------------------------------------------------
 */

namespace app\api\controller;

use app\api\server\member\memberServer;
use app\api\server\member\myOrderServer;
use app\api\server\storeServer;
use think\Controller;
use app\common\common\lib\OpensslEncrypt;
use app\common\common\model\oldModel\member\memberCard;
use app\common\common\Server\oldMemberServer;
use app\common\common\Server\commonServer;
use think\response\Json;
use app\common\common\Server\TemplateMessageServer;
use think\Env;
use app\common\common\model\order\fromId;
use app\common\common\model\oldModel\order\order as orderModel;

class  Order extends Base {


    /**
     * 订单列表
     * @param $sta 标示 0为待评价 1为全部列表
     * @param $currentPage 第几页
     * @param $version 版本号
     * @return Json
     */
    public function getOrderList(){

        try{

            $sta = input('sta');
            $key = input('key');
            $currentPage = input('currentPage',0);
            $version = input('version' , 0);

            if((empty($sta) && !isset($sta)) || empty($key)){

                commonServer::error(2)->exits();
            }
            $length = 10;
            if($version == 0){
                $length = 100;
            }
            $limit = $currentPage*$length.','.$length;
            $member_server = new memberServer();
            $member_info = $member_server->checkCiphertext($key);
            if($member_info === false){
                commonServer::error(2, [],'密钥不合法')->exits();
            }

            //根据新的member_id获取旧的用户member_id
            $memberServer = new memberServer();
            $member_old_info = $memberServer -> getOldMemberByNewId($member_info['id']);
            //查找订单列表数据
            $order_server = new myOrderServer();
            $data = $order_server->orderlist($sta,$limit,$member_old_info['member_id']);

            commonServer::success($data['data'], '成功',array('count'=>$data['count']))->exits();
        }catch (\Exception $e){
            $err = formatErr($e);
            commonServer::error(99997,[], $err)->exits();
        }
    }


    /**
     * 用户订单评价调用旧平台接口
     */
    public function evaluate(){

        $input=input('post.');
        aw_log('获取数据成功1'.json_encode($input) ,'canxingjian2.log');
        if (empty($input)){
            commonServer::error(2)->exits();
        }


        if(!empty($input['formId'])){

            if(empty($input['key'])|| empty($input['openId'])){
                commonServer::error(2)->exits();
            }
            $member_server = new memberServer();
            //验证key合法
            $member_info = $member_server->checkCiphertext($input['key']);
            if($member_info === false){
                commonServer::error(2, [],'密钥不合法')->exits();
            }

            //添加formId
            $memberServer = new memberServer();
            $memberServer->addUserNewFormId($member_info['id'],$input['openId'],$input['formId']);

        }
        $input['itemIdArr'] = json_encode($input['itemIdArr']);
        try {
            $url=Env::get('old.url', 'http://jhdcoldtest.canxunnet.com').'/home/member/doComment';
            $return=postcurl($url, $input);
            return $return;

        }catch (\Exception $e){
            $err = formatErr($e);
            commonServer::error(99997,[], $err)->exits();
        }
    }

    /**
     * 订单评价
     */
    public function getOrderGoods(){
        $order_id = input('order_id');
        try{
            if(empty($order_id)){

                commonServer::error(2)->exits();
            }
            $order_server = new myOrderServer();
            $data = $order_server->orderGoods($order_id);
            commonServer::success($data, '成功')->exits();

        }catch (\Exception $e){
            $err = formatErr($e);
            commonServer::error(99997,[], $err)->exits();
        }

    }

    /**
     * 订单详情
     * @param $order_id 订单id
     * @return Json
     */
    public function getOrderItem(){

        try{
            $order_id = input('order_id');

            if(empty($order_id)){

                commonServer::error(2)->exits();
            }

            $order_server = new myOrderServer();
            $data=$order_server->orderItem($order_id);

            commonServer::success($data, '成功')->exits();
        }catch (\Exception $e){
            $err = formatErr($e);
            commonServer::error(99997,[], $err)->exits();
        }

    }
    /**
     * 取消订单
     * @param $order_id 取消订单
     * @return Json
     */
    public function cancelOrder(){

        try{

            $order_id = input('order_id');
            $key = input('key');

            if( empty ($order_id) || empty ($key)){

                commonServer::error(2)->exits();
            }
            $member_server = new memberServer();
            //验证key合法
            $member_info = $member_server->checkCiphertext($key);
            if($member_info === false){
                commonServer::error(2, ['surplus'=>0],'密钥不合法')->exits();
            }

            //根据新的member_id获取旧的用户member_id
            $memberServer = new memberServer();
            $member_old_info = $memberServer -> getOldMemberByNewId($member_info['id']);
            //取消订单
            $order_server = new myOrderServer();
            $res = $order_server->cancelOrder($order_id,$member_old_info['member_id']);
            commonServer::success($res, '成功')->exits();
        }catch (\Exception $e){
            $err = formatErr($e);
            commonServer::error(99997,[], $err)->exits();
        }

    }

    /**
     * 获取早餐卡和共享卡的列表数据，并且带上嘉和卡 的余额
     */
    public function getPayList(){

        //校验密钥，通过nick_name
        $nick_name = input('nick_name','');
        $key = input('key','');
        $store_id = input('store_id','');
        $classify_name = input('classify_name','');
        $goods_num = input('goods_num','');

        //验证接收值的正确与否
        $res = $this->validate(input(),'GetPayList');

        if(true !== $res){
            commonServer::$lastShowMsg = $res;
            commonServer::error(2)->dies();
        }

        $member_server = new memberServer();
        //验证key合法
        $member_info = $member_server->checkCiphertext($key);
        aw_log($member_info,'canxingjian.log');
        if($member_info === false){
            commonServer::error(2, ['surplus'=>0],'密钥不合法')->exits();
        }

        //todo aw: 需要查询出嘉和卡的余额,未测试
        $old_member = $member_server->getOldMemberByNewId($member_info['id']);
        //旧平台没有该用户
        if(empty($old_member)){
            commonServer::$showMsg = '用户数据异常';
            commonServer::error(10002)->dies();
        }
        aw_log($old_member ,'canxingjian.log');
        $jiahe_res = $member_server->getMemberByCanxinjian($old_member);
        if($jiahe_res == false){
            $jiahe_card['surplus'] = 0;
        }else{
            if($jiahe_res['success']){
                $jiahe_card['surplus'] = $jiahe_res['memberinfo'][0]['totalbalance'] > 0 ? floatval($jiahe_res['memberinfo'][0]['totalbalance']) : 0;
            }else{
                $jiahe_card['surplus'] = 0;
            }
        }
        aw_log($jiahe_card,'canxingjian.log');
        //判断是否是早餐时段，可以不可以使用早餐券等
        $status_arr = $member_server->isUseBreakfastCade($store_id, $classify_name, $old_member, $goods_num);
        if($status_arr['code'] != 0){
            commonServer::error($status_arr['code'], $jiahe_card)->dies();
        }

        $member_info['member_card_arr'] = array_column($status_arr['data'],'card_id');
        $key = $member_server->encryptionMemberInfo($member_info);
        $jiahe_card['card_list'] = $status_arr['data'];
        commonServer::success($jiahe_card, '', ['key'=>$key])->exits();
    }


    /**
     * 订单消息推送
     * @param order_code 订单单号
     * @param type_send 推送类型 1为退款推送通知 2 交易成功通知 3 用户评价通知
     *
     */
    public function orderSendMessage(){

        $order_code = input('order_code');
        $type_send = input('type_send');
        $log['code'] = $order_code;
        $log['type'] = $type_send;
        aw_log($log, 'message_stend.log');
        try{
            if( empty ($type_send) || empty ($order_code) ){
                commonServer::error(2,[])->exits();
            }

            $TemplateMessageServer = new TemplateMessageServer();
            $res = $TemplateMessageServer->sendMessage($order_code,$type_send);
            if($res['errcode'] == 0){
                commonServer::success($res['errmsg'], '', [])->exits();
            }else{
                commonServer::error(99999,$res)->exits();
            }
        }catch (\Exception $e){
            $err = formatErr($e);
            commonServer::error(99997,[], $err)->exits();
        }

    }







}